﻿using Furion.DynamicApiController;
using Furion.UnifyResult;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using MSEAP.Entitys;
using MSEAP.VO.Project;
using SqlSugar;

namespace MSEAP.Controllers
{
    [DynamicApiController]
    public class ProjectController
    {
        private readonly ILogger<ProjectController> logger;
        private readonly ISqlSugarClient db;

        public ProjectController(ILogger<ProjectController> logger, ISqlSugarClient db)
        {
            this.db = db;
            this.logger = logger;
        }

        /// <summary>
        /// 项目列表
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> List([FromBody] ListVO vo)
        {
            RefAsync<int> total = 0;
            List<ProjectEntity> projects = await db.Queryable<ProjectEntity>()
                .Where(pe => pe.IsActive == 1)
                .WhereIF(!string.IsNullOrEmpty(vo.Content), pe => pe.Name.Contains(vo.Content))
                .Mapper(async pe => { 
                    pe.User = await db.Queryable<UserEntity>().InSingleAsync(pe.UId);
                    pe.Users = await db.Queryable<UserEntity>().Where(ue => pe.UIds.Contains(ue.Id)).ToListAsync();
                })
                .ToPageListAsync(vo.PageIndex, vo.PageSize, total);

            UnifyContext.Fill(new { Total = total.Value });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = projects,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 增加项目
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Save([FromBody] SaveVO vo)
        {
            await db.Insertable<ProjectEntity>(vo.Adapt<ProjectEntity>()).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = vo,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete([FromBody] DeleteVO vo)
        {
            await db.Updateable<ProjectEntity>().SetColumns(u => u.IsActive == 0).Where(u => u.Id == vo.Id).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 修改项目
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update([FromBody] UpdateVO vo)
        {
            await db.Updateable<ProjectEntity>(vo).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = vo,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看项目
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Show([FromBody] ShowVO vo)
        {
            ProjectEntity project = await db.Queryable<ProjectEntity>()
                .Where(pe => pe.IsActive == 1)
                .Mapper(async pe => {
                    pe.User = await db.Queryable<UserEntity>().InSingleAsync(pe.UId);
                    pe.Users = await db.Queryable<UserEntity>().Where(ue => pe.UIds.Contains(ue.Id)).ToListAsync();
                })
                .InSingleAsync(vo.Id);

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = project,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }
    }
}
